package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.PmsProductAttributeCategoryMapper;
import com.mall.db.mapper.PmsProductAttributeMapper;
import com.mall.db.mapper.PmsProductCategoryAttributeRelationMapper;
import com.mall.domain.PmsBrand;
import com.mall.domain.PmsProductAttribute;
import com.mall.domain.PmsProductCategoryAttributeRelation;
import com.mall.interface1.api.PmsProductAttributeApi;
import com.mall.interface1.api.PmsProductAttributeCategoryApi;
import com.mall.vo.PmsProductAttributeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class PmsProductAttributeApiImpl implements PmsProductAttributeApi {

    @Autowired
    private PmsProductAttributeMapper attributeMapper;
    @Autowired
    private PmsProductAttributeCategoryMapper categoryMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper relationMapper;

    //根据商品分类的id获取商品属性及属性分类
    @Override
    public List<Map<String, Integer>> productCategory(Integer productCategoryId) {
        List<Map<String, Integer>> mapList = new ArrayList<>();
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId, productCategoryId);
        List<PmsProductCategoryAttributeRelation> pmsProductCategoryAttributeRelations = relationMapper.selectList(wrapper);
        for (PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation : pmsProductCategoryAttributeRelations) {
            Map<String, Integer> map = new HashMap<>();
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            Long productAttributeId = pmsProductCategoryAttributeRelation.getProductAttributeId();
            queryWrapper.eq(PmsProductAttribute::getId, productAttributeId);
            PmsProductAttribute productAttribute = attributeMapper.selectOne(queryWrapper);
            Long productAttributeCategoryId = productAttribute.getProductAttributeCategoryId();
            map.put("attributeId", Integer.valueOf(productAttributeId.toString()));
            map.put("attributeCategoryId", Integer.valueOf(productAttributeCategoryId.toString()));
            mapList.add(map);
        }
        return mapList;
    }

    //添加商品属性信息
    @Override
    public void create(PmsProductAttribute productAttribute) {
        attributeMapper.insert(productAttribute);
    }

    //批量删除商品属性
    @Override
    public void deleteAttributeByIds(List<Long> ids) {
        // 删除属性表中相应的数据
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProductAttribute::getId, ids);
        attributeMapper.delete(wrapper);
        // 删除属性分类联系表
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> relationWrapper = new LambdaQueryWrapper<>();
        relationWrapper.in(PmsProductCategoryAttributeRelation::getProductAttributeId, ids);
        relationMapper.delete(relationWrapper);
    }

    //修改商品属性信息
    @Override
    public void updateAttribute(Long id, PmsProductAttribute productAttribute) {
        LambdaQueryWrapper<PmsProductAttribute>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getId,id);
        attributeMapper.update(productAttribute,wrapper);
    }

    //查询单个商品属性
    @Override
    public PmsProductAttribute productAttributeById(Integer id) {
        LambdaQueryWrapper<PmsProductAttribute>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getId,id);
        return attributeMapper.selectOne(wrapper);
    }

    //根据分类查询属性列表或参数列表
    @Override
    public Page<PmsProductAttribute> attributeList(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        Page<PmsProductAttribute> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,Long.valueOf(cid));
        wrapper.eq(PmsProductAttribute::getType,type);
        Page<PmsProductAttribute> page = attributeMapper.selectPage(pageInfo, wrapper);
        return page;
    }


}
